package com.platform.module.common.base;

import com.alibaba.fastjson2.JSONObject;
import io.swagger.v3.oas.annotations.media.Schema;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Objects;

@Schema(description = "通用响应")
public class CommonResp<T> extends Response {
    private static final Logger log = LoggerFactory.getLogger(CommonResp.class);
    @Schema(description = "响应数据，类型不固定")
    private T data;

    public T getData() {
        return this.data;
    }

    public CommonResp<T> setData(T data) {
        this.data = data;
        return this;
    }

    public CommonResp() {
    }

    public CommonResp(CodeDict code) {
        super(code);
    }

    public CommonResp(String code, String msg, T data) {
        super(code, msg);
        this.data = data;
    }

    public CommonResp(String code, String msg) {
        super(code, msg);
    }

    public boolean isSuccess() {
        return Objects.equals(CodeDict.SUCCESS.getCode(), this.getCode());
    }

    public static <T> CommonResp<T> ok() {
        return new CommonResp<>(CodeDict.SUCCESS.getCode(), CodeDict.SUCCESS.getMsg());
    }

    public static <T> CommonResp<T> ok(String msg) {
        return new CommonResp<>(CodeDict.SUCCESS.getCode(), msg);
    }

    public static <T> CommonResp<T> ok(String msg, T data) {
        return new CommonResp<>(CodeDict.SUCCESS.getCode(), msg, data);
    }

    public static <T> CommonResp<T> ok(T data) {
        return new CommonResp<>(CodeDict.SUCCESS.getCode(), "success", data);
    }


    public static <T> CommonResp<T> ok(CodeDict codeDict, T data) {
        return new CommonResp<>(codeDict.getCode(), codeDict.getMsg(), data);
    }

    public static <T> CommonResp<T> ok(String code, String msg, T data) {
        return new CommonResp<>(code, msg, data);
    }

    public static <T> CommonResp<T> fail() {
        return new CommonResp<>(CodeDict.FAILED.getCode(), CodeDict.FAILED.getMsg());
    }

    public static <T> CommonResp<T> fail(String msg) {
        return new CommonResp<>(CodeDict.FAILED.getCode(), msg);
    }


    public static <T> CommonResp<T> fail(String code, String msg) {
        return new CommonResp<>(code, msg);
    }

    public static <T> CommonResp<T> fail(CodeDict codeDict) {
        return new CommonResp<>(codeDict.getCode(), codeDict.getMsg());
    }

    public static <T> CommonResp<T> fail(CodeEnum codeDict, Object data) {
        return new CommonResp<>(codeDict.getCode(), String.format(codeDict.getMsg(), data));
    }


    public static <T> CommonResp<T> fail(CodeEnum codeEnum) {
        return new CommonResp<>(codeEnum.getCode(), codeEnum.getMsg());
    }


    public String toString() {
        return JSONObject.toJSONString(this);
    }
}
